#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename	:  ipc_test
# Version	:  1.0
# Date		:  2023/11/30
# Author	:  yaoxiyao
# Email		:  yaoxiyao@kylinos.com.cn 
# History	:     
#              Version 1.0, 2023/11/30
# Function	:  共享内存的创建、挂接和释放
# Out		:        
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------

# 测试主题
Title_Env_LTFLIB="共享内存的创建、挂接和释放"

## TODO : 个性化,初始化
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestInit_LTFLIB(){
        IpcmkFile="${TmpTestDir_LTFLIB}/ipcmk.txt"
        IpckeyFile="${TmpTestDir_LTFLIB}/ipckey.txt"
        IpcrmFile="${TmpTestDir_LTFLIB}/ipcrm.txt"

        touch ${IpcmkFile}
        CommRetParse_FailDiy_LTFLIB ${ERROR} "创建文件失败${IpcmkFile}"
        touch ${IpckeyFile}
        CommRetParse_FailDiy_LTFLIB ${ERROR} "创建文件失败${IpckeyFile}"
        touch ${IpcrmFile}
		CommRetParse_FailDiy_LTFLIB ${ERROR} "创建文件失败${IpcrmFile}"
		return $TPASS
}


## TODO : 清理函数
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestClean_LTFLIB(){
		Debug_LLE "rm -rf ${IpcmkFile} ${IpckeyFile} ${IpcrmFile}"
		rm -rf ${IpcmkFile} ${IpckeyFile} ${IpcrmFile}
        return $TPASS
}


## TODO : 测试用例集
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
Testsuite_LTFLIB(){
        testcase_1
        testcase_2
        testcase_3
        return $TPASS
}


## TODO ： 共享内存的创建、挂接和释放 
# 查看共享内存信息
testcase_1(){
        ipcs
        CommRetParse_LTFLIB "ipcs显示消息队列、信号集、或者共享内存的标识"
}

# 添加共享内存
testcase_2(){
        ipcmk -M 100 >> ${IpcmkFile}
        CommRetParse_LTFLIB "添加共享内存"      
}

# 获取共享内存id
testcase_3(){
        local Shmid=`cat ${IpcmkFile}|tr -cd '[0-9]'`
        CommRetParse_LTFLIB "获取共享内存id ${Shmid}"

        ipcs >> ${IpckeyFile}
        local Key=`grep -E ${Shmid} ${IpckeyFile}|awk -F' ' '{print $1}'`
        CommRetParse_LTFLIB "获取共享内存key"

        ipcrm -M ${Key}
        CommRetParse_LTFLIB "删除共享内存"

        ipcs >> ${IpcrmFile}
        local KeyShmid=`grep -E "${Key}|${Shmid}" ${IpcrmFile}`
        [ "Z${KeyShmid}" == "Z" ]
        CommRetParse_LTFLIB "成功删除共享内存"
}

#----------------------------------------------#

source "${LIB_LTFLIB}"
Main_LTFLIB $@
